import pygame
import random

# 初始化 Pygame
pygame.init()

# 定义常量
WIDTH = 600
HEIGHT = 600
BLOCK_SIZE = 30
PACMAN_SIZE = 30
GHOST_SIZE = 30
WHITE = (255, 255, 255)
YELLOW = (255, 255, 0)
RED = (255, 0, 0)
BLACK = (0, 0, 0)

# 创建游戏窗口
screen = pygame.display.set_mode((WIDTH, HEIGHT))
pygame.display.set_caption("吃豆人游戏")

# 迷宫地图（0 表示通道，1 表示墙壁，2 表示豆子）
maze = [
    [1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1],
    [1, 2, 2, 2, 2, 2, 2, 2, 2, 1, 2, 2, 2, 2, 2, 2, 2, 2, 2, 1],
    [1, 2, 1, 1, 1, 2, 1, 1, 2, 1, 2, 1, 1, 2, 1, 1, 1, 2, 2, 1],
    [1, 2, 1, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 1, 2, 2, 1],
    [1, 2, 1, 2, 1, 1, 2, 1, 1, 1, 1, 1, 2, 1, 1, 2, 1, 2, 2, 1],
    [1, 2, 2, 2, 2, 2, 2, 2, 2, 1, 2, 2, 2, 2, 2, 2, 2, 2, 2, 1],
    [1, 2, 1, 2, 1, 1, 2, 1, 1, 1, 1, 1, 2, 1, 1, 2, 1, 2, 2, 1],
    [1, 2, 1, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 1, 2, 2, 1],
    [1, 2, 1, 1, 1, 2, 1, 1, 2, 1, 2, 1, 1, 2, 1, 1, 1, 2, 2, 1],
    [1, 2, 2, 2, 2, 2, 2, 2, 2, 1, 2, 2, 2, 2, 2, 2, 2, 2, 2, 1],
    [1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1]
]

# 吃豆人类
class Pacman:
    def __init__(self, x, y):
        self.x = x
        self.y = y
        self.direction = 'right'

    def move(self, direction):
        new_x, new_y = self.x, self.y
        if direction == 'up':
            new_y -= 1
        elif direction == 'down':
            new_y += 1
        elif direction == 'left':
            new_x -= 1
        elif direction == 'right':
            new_x += 1
        if 0 <= new_x < len(maze[0]) and 0 <= new_y < len(maze) and maze[new_y][new_x] != 1:
            self.x = new_x
            self.y = new_y
            self.direction = direction

    def draw(self):
        pygame.draw.circle(screen, YELLOW, (self.x * BLOCK_SIZE + BLOCK_SIZE // 2, self.y * BLOCK_SIZE + BLOCK_SIZE // 2),
                           PACMAN_SIZE // 2)

# 幽灵类
class Ghost:
    def __init__(self, x, y):
        self.x = x
        self.y = y

    def move(self):
        directions = ['up', 'down', 'left', 'right']
        valid_directions = []
        for direction in directions:
            new_x, new_y = self.x, self.y
            if direction == 'up':
                new_y -= 1
            elif direction == 'down':
                new_y += 1
            elif direction == 'left':
                new_x -= 1
            elif direction == 'right':
                new_x += 1
            if 0 <= new_x < len(maze[0]) and 0 <= new_y < len(maze) and maze[new_y][new_x] != 1:
                valid_directions.append(direction)
        if valid_directions:
            chosen_direction = random.choice(valid_directions)
            if chosen_direction == 'up':
                self.y -= 1
            elif chosen_direction == 'down':
                self.y += 1
            elif chosen_direction == 'left':
                self.x -= 1
            elif chosen_direction == 'right':
                self.x += 1

    def draw(self):
        pygame.draw.rect(screen, RED,
                         (self.x * BLOCK_SIZE, self.y * BLOCK_SIZE, GHOST_SIZE, GHOST_SIZE))

# 创建吃豆人和幽灵
pacman = Pacman(1, 1)
ghosts = [Ghost(18, 1), Ghost(1, 9)]

# 得分
score = 0

# 游戏主循环
clock = pygame.time.Clock()
running = True
while running:
    screen.fill(BLACK)

    # 处理事件
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            running = False
        elif event.type == pygame.KEYDOWN:
            if event.key == pygame.K_UP:
                pacman.move('up')
            elif event.key == pygame.K_DOWN:
                pacman.move('down')
            elif event.key == pygame.K_LEFT:
                pacman.move('left')
            elif event.key == pygame.K_RIGHT:
                pacman.move('right')

    # 绘制迷宫
    for y in range(len(maze)):
        for x in range(len(maze[0])):
            if maze[y][x] == 1:
                pygame.draw.rect(screen, WHITE, (x * BLOCK_SIZE, y * BLOCK_SIZE, BLOCK_SIZE, BLOCK_SIZE))
            elif maze[y][x] == 2:
                pygame.draw.circle(screen, WHITE,
                                   (x * BLOCK_SIZE + BLOCK_SIZE // 2, y * BLOCK_SIZE + BLOCK_SIZE // 2), 5)

    # 检查吃豆人是否吃到豆子
    if maze[pacman.y][pacman.x] == 2:
        maze[pacman.y][pacman.x] = 0
        score += 1

    # 移动和绘制幽灵
    for ghost in ghosts:
        ghost.move()
        ghost.draw()
        if ghost.x == pacman.x and ghost.y == pacman.y:
            running = False

    # 绘制吃豆人
    pacman.draw()

    # 显示得分
    font = pygame.font.Font(None, 36)
    score_text = font.render(f"得分: {score}", True, WHITE)
    screen.blit(score_text, (10, 10))

    # 更新显示
    pygame.display.flip()
    clock.tick(3)

# 退出游戏
pygame.quit()